package com.pactera.madp.cp.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.cp.api.entity.WorkDeptEntity;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.entity.crm.WorkAssetsInfoEntity;
import com.pactera.madp.cp.api.vo.MgrCustAssetsVo;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.mapper.MgrCustMapper;
import com.pactera.madp.cp.service.IWorkDeptService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import com.pactera.madp.cp.service.MgrCustService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class MgrCustServiceImpl implements MgrCustService {

    private final MgrCustMapper mgrCustMapper;
    private final IWorkEmployeeService employeeService;
    private final IWorkDeptService workDeptService;

    @Override
    public MgrCustAssetsVo getMgrCustAssets(String jobNo) {
        // 统计资产
        List<String> custTableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        List<String> assetsTableNames = SyncUtils.getTableNames(Constants.TP_WORK_ASSETS_INFO, Constants.TC_ASSETS_INFO);

        Map<String, String> tableNameMap = new LinkedHashMap<>();
        for (int i=0; i<custTableNames.size(); i++) {
            tableNameMap.put(custTableNames.get(i), assetsTableNames.get(i));
        }

        List<WorkAssetsInfoEntity> assetsList = mgrCustMapper.getMgrCustAssets(jobNo, tableNameMap);

        MgrCustAssetsVo vo = statisticAssets(assetsList);

        // 设置数据日期
        if (!assetsList.isEmpty()) {
            String dataDt = assetsList.get(0).getCreateTime().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            vo.setDataDt(dataDt);
        }

        // 统计管户数
        int count = mgrCustMapper.getMgrCustCount(jobNo, custTableNames);
        vo.setMgrCustCount(count);

        // 获取管户人信息
        WorkEmployeeEntity employ = employeeService.getOne(Wrappers.<WorkEmployeeEntity>lambdaQuery()
                .eq(WorkEmployeeEntity::getWxUserId, jobNo));
        vo.setAvatar(employ.getAvatar());
        vo.setMgrName(employ.getName());
        WorkDeptEntity dept = workDeptService.getOne(Wrappers.<WorkDeptEntity>lambdaQuery()
                .eq(WorkDeptEntity::getWxDepartmentId, employ.getWxMainDepartmentId()));
        String[] deptIds = dept.getPath().replace("#", "").split("-");
        List<WorkDeptEntity> deptList = workDeptService.list(Wrappers.<WorkDeptEntity>lambdaQuery()
                .in(WorkDeptEntity::getId, deptIds)
                .orderByAsc(WorkDeptEntity::getLevel));
        vo.setOrgName("");
        deptList.forEach(d -> vo.setOrgName(vo.getOrgName() + d.getName() + "-"));
        vo.setOrgName(vo.getOrgName().substring(0, vo.getOrgName().length() - 1));

        return vo;
    }

    // 计算资产总额
    private MgrCustAssetsVo statisticAssets(List<WorkAssetsInfoEntity> assetsList) {
        // 分类总资产
        WorkAssetsInfoEntity assets = new WorkAssetsInfoEntity();
        assets.setAumBal(BigDecimal.ZERO);
        assets.setDeptBal(BigDecimal.ZERO);
        assets.setLoanBal(BigDecimal.ZERO);
        assets.setFinProdBal(BigDecimal.ZERO);
        assets.setFundBal(BigDecimal.ZERO);
        assets.setInsureBal(BigDecimal.ZERO);
        assets.setAmPlanBal(BigDecimal.ZERO);
        assets.setTrustBal(BigDecimal.ZERO);

        // 分类资产统计
        assetsList.forEach(a -> {
            assets.setAumBal(assets.getAumBal().add(a.getAumBal()));
            assets.setDeptBal(assets.getDeptBal().add(a.getDeptBal()));
            assets.setLoanBal(assets.getLoanBal().add(a.getLoanBal()));
            assets.setFinProdBal(assets.getFinProdBal().add(a.getFinProdBal()));
            assets.setFundBal(assets.getFundBal().add(a.getFundBal()));
            assets.setInsureBal(assets.getInsureBal().add(a.getInsureBal()));
            assets.setAmPlanBal(assets.getAmPlanBal().add(a.getAmPlanBal()));
            assets.setTrustBal(assets.getTrustBal().add(a.getTrustBal()));
        });

        // 设置分类资产总额
        MgrCustAssetsVo vo = new MgrCustAssetsVo();
        vo.setAumBal(assets.getAumBal());
        vo.setDeptBal(assets.getDeptBal());
        vo.setLoanBal(assets.getLoanBal());
        vo.setFinProdBal(assets.getFinProdBal());
        vo.setFundBal(assets.getFundBal());
        vo.setInsureBal(assets.getInsureBal());
        vo.setAmPlanBal(assets.getAmPlanBal());
        vo.setTrustBal(assets.getTrustBal());
        return vo;
    }
}
